package com.hxzy.shop.commodity.controller;

import com.github.pagehelper.PageInfo;
import com.hxzy.shop.commodity.dto.req.*;
import com.hxzy.shop.commodity.dto.resp.DrugResp;
import com.hxzy.shop.commodity.dto.resp.ImportDrugResp;
import com.hxzy.shop.commodity.dto.resp.MarketingDrugListResp;
import com.hxzy.shop.commodity.service.DrugService;
import com.hxzy.shop.entity.Drug;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhy
 * @version 1.0
 * @Date 2021/07/07 9:59
 */
@RestController
@Api(tags = "药品")
@RequestMapping("/drug")
public class DrugController {

    @Resource
    private DrugService drugService;


    /**
     * 药品分页
     * @param drugPageReq 药品分页请求
     * @return 返回分页集合
     */
    @ApiOperation("获得商户分页是数据")
    @PostMapping("/getDrugPage")
    public Result<PageInfo<Drug>> getDrugPage(@ApiParam(required = true,name="drugPageReq",value = "药品分页请求model")
                                              @RequestBody DrugPageReq drugPageReq){
        PageInfo<Drug> drugPageInfo=drugService.getDrugPage(drugPageReq);
        return Result.success(drugPageInfo);
    }

    /**
     * 添加商品
     *
     * @param addDrugReq 添加商品
     * @return 是否成功
     */
    @ApiOperation("添加商品or修改")
    @PostMapping("/addDrug")
    public Result<Boolean> addDrug(@ApiParam(required = true,name = "addDrugReq",value = "添加商品请求model")
                                   @Validated @RequestBody AddDrugReq addDrugReq){
        boolean result=drugService.addDrug(addDrugReq);
        if (result) {
            return Result.success(result);
        } else {
            return Result.failed(ResultCode.FAILED, "添加失败");
        }
    }
    /**
     * 根据分类获得药品
     */
    @ApiOperation("根据分类获得药品")
    @PostMapping("/getDrugPageByClassification")
    public Result<PageInfo<Drug>> getDrugPageByClassification(@RequestBody ClassificationDrugPageReq req){

        PageInfo<Drug> pageInfo =drugService.getDrugPageByClassification(req);
        return Result.success(pageInfo);
    }


    /**
     * @param stock 修改库存
     * @author qy
     * @date 2021/8/4
     **/
    @ApiOperation("修改库存")
    @PostMapping("/updateStock")
    public Result<Boolean> updateStock(@ApiParam(readOnly = true, name = "stock", value = "库存")
                                       @RequestBody @Validated DrugUpdateReq stock) {
        boolean result = drugService.updateStock(stock);
        if (result) {
            return Result.success(result);
        } else {
            return Result.failed(ResultCode.FAILED, "修改失败");
        }

    }
    @PostMapping("/marketing/list")
    @ApiOperation("获取药物分页")
    public Result<PageInfo<MarketingDrugListResp>> findPageDrug(@RequestBody DrugPageReq drugPageReq) {
        return Result.success(drugService.getPrecinctDrug(drugPageReq));
    }
    /**
     * 删除药品
     *
     * @param idS 药品id
     * @return 是否成功
     */
    @ApiOperation("删除药品")
    @PostMapping("/deleteDrug")
    public Result<Boolean> deleteDrug(@ApiParam(required = true, name = "ids", value = "删除的id数组")
                                      @RequestBody List<Long> idS) {
        boolean result = drugService.deleteDrug(idS);
        return Result.success(result);
    }
    /**
     * 更新状态
     *
     * @param state 状态
     * @return 是否成功
     */
    @ApiOperation("更新状态")
    @PostMapping("/updateState")
    public Result<Boolean> updateState(@ApiParam(readOnly = true, name = "state", value = "状态")
                                       @RequestBody DrugUpdateReq state) {
        boolean result = drugService.updateState(state);
        return Result.success(result);
    }
    /**
     * 获得药品详细
     *
     * @param req 药品id
     * @return 药品详细
     */
    @ApiOperation("获得药品详细")
    @PostMapping("/getOneDrug")
    public Result<DrugResp> getOneDrug(@RequestBody DrugInfoReq req) {
        DrugResp result = drugService.getOneDrug(req);
        return Result.success(result);
    }
    /**
     * 批量上下架
     */
    @ApiOperation("批量上下架")
    @PostMapping("/updateBatchState")
    public Boolean updateBatchState(@RequestBody EditClassifyDto dto) {
        return drugService.updateBatchState(dto);
    }
    /**
     * 批量修改分类
     */
    @ApiOperation("批量修改分类")
    @PostMapping("/updateClassify")
    public Result<Boolean> updateClassify(@RequestBody EditClassifyDto dto) {
       return Result.success(drugService.updateClassify(dto));
    }
    /**
     * 批量修改药品信息
     */
    @ApiOperation("批量修改药品信息")
    @PostMapping("/batchEditDrugDetail")
    public Result<ImportDrugResp> batchEditDrugDetail(MultipartFile file){
        return Result.success(drugService.batchEditDrugDetail(file));
    }
}
